package com.hsy.crm.web.web.basics;

import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.Response;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Product;
import com.hsy.crm.web.domain.vo.product.ProductInsertVo;
import com.hsy.crm.web.domain.vo.product.ProductPageVo;
import com.hsy.crm.web.domain.vo.unit.ProductUnitVo;
import com.hsy.crm.web.service.base.CacheService;
import com.hsy.crm.web.service.impl.ProductService;
import com.hsy.crm.web.web.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/product")
public class ProductAction extends BaseAction {

    @Autowired
    ProductService productService;

    @RequestMapping("/insert")
    public String insert(ProductInsertVo productInsertVo) {
        Unified<Integer> unified = productService.insertProduct(productInsertVo);
        if( unified.getResult() == App.Result.SUCCESS ){
            CacheService.putProductId(productInsertVo.getCustomerId(),productInsertVo.getProduct().getId());
        }
        return Response.body(unified);
    }

    @RequestMapping("/update")
    public String update(ProductInsertVo productInsertVo) {
        return Response.body(productService.updateProduct(productInsertVo));
    }

    @RequestMapping("/delete")
    public String delete(Product product) {
        Unified<Integer> unified = productService.deleteProduct(product);
        if( unified.getResult() == App.Result.SUCCESS ){
            CacheService.removeProductId(product.getCustomerId(),product.getId());
        }
        return Response.body(unified);
    }

    @RequestMapping("/select")
    public String select(Product product) {
        return Response.body(productService.select(product));
    }

    @RequestMapping("/get")
    public String get(Product product) {
        return Response.body(productService.get(product));
    }

    @RequestMapping("/getByBarcode")
    public String getByBarcode(ProductPageVo productPageVo) {
        return Response.body(Unified.newInstance(productService.getByBarcode(productPageVo)));
    }

    @RequestMapping("/getUnit")
    public String getUnit(ProductUnitVo productUnitVo) {
        return Response.body(productService.getUnit(productUnitVo));
    }

    @RequestMapping("/getMultiUnit")
    public String getMultiUnit(ProductUnitVo productUnitVo) {
        return Response.body(productService.getMultiUnit(productUnitVo));
    }

    @RequestMapping("/page")
    public String page(ProductPageVo productPageVo) {
        return Response.body(productService.page(productPageVo));
    }

    @RequestMapping("/choose")
    public String choose(ProductPageVo productPageVo) {
        return Response.body(productService.choose(productPageVo));
    }

    /**
     * 好像没有用
     * @param productPageVo
     * @return
     */
    @RequestMapping("/chose")
    public String chose(ProductPageVo productPageVo) {
        return Response.body(productService.chose(productPageVo));
    }

}
